package com.hhkj.study.admin.resource.service.extend.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.hhkj.study.admin.course.enums.CourseAssocResourceTypeEnum;
import com.hhkj.study.admin.resource.entity.ResourceCourseBase;
import com.hhkj.study.admin.resource.entity.ResourceCourseLive;
import com.hhkj.study.admin.resource.model.podo.ResourceCourseLiveDo;
import com.hhkj.study.admin.resource.model.podo.ResourceCourseLivePo;
import com.hhkj.study.admin.resource.model.podo.ResourceCourseVideoPo;
import com.hhkj.study.admin.resource.service.base.ResourceCourseLiveService;
import com.hhkj.study.admin.resource.service.extend.ResourceCourseBaseExtendService;
import com.hhkj.study.admin.resource.service.extend.ResourceCourseLiveExtendService;
import com.hhkj.study.admin.resource.service.extend.ResourceCourseVideoExtendService;
import com.ruoyi.common.base.biz.service.extend.BaseExtendServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
*
* @author SERVER
* @since 2024-09-06
*/
@Service
public class ResourceCourseLiveExtendServiceImpl extends BaseExtendServiceImpl<ResourceCourseLive, ResourceCourseLiveDo, ResourceCourseLivePo, ResourceCourseLiveService>
        implements ResourceCourseLiveExtendService, ResourceCourseBaseExtendService {

    @Resource
    private ResourceCourseVideoExtendService videoExtendService;


    @Override
    public ResourceCourseLive getByNameCourseType(String name, Long courseType) {
        LambdaQueryWrapper<ResourceCourseLive> liveQuery = new LambdaQueryWrapper<>();
        liveQuery.eq(ResourceCourseLive::getName, name).eq(ResourceCourseLive::getCourseType, courseType).last("LIMIT 1");
        return this.getService().getOne(liveQuery);
    }

    @Override
    public CourseAssocResourceTypeEnum getCourseResourceTypeEnum() {
        return CourseAssocResourceTypeEnum.LIVE;
    }

    @Override
    public List<ResourceCourseLivePo> listByIdList(Collection<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return Lists.newArrayList();
        }
        ResourceCourseLiveDo resourceCourseLiveDo = new ResourceCourseLiveDo();
        resourceCourseLiveDo.setIdList(Lists.newArrayList(idList));
        List<ResourceCourseLivePo> resourceCourseLivePoList = this.getService().listPoByDo(resourceCourseLiveDo);
        afterLiveVideo(resourceCourseLivePoList);
        return resourceCourseLivePoList;
    }

    @Override
    public List<ResourceCourseLive> getResouceListByIdList(Collection<? extends Serializable> idList, List<? extends ResourceCourseBase> resourceList) {
        if (CollectionUtil.isEmpty(idList) || CollectionUtil.isEmpty(resourceList)) {
            return Lists.newArrayList();
        }
        List<ResourceCourseLive> resourceCourseList = BeanUtil.copyToList(resourceList, ResourceCourseLive.class);
        return resourceCourseList.stream().filter(e -> idList.contains(e.getId())).collect(Collectors.toList());
    }

    public void afterLiveVideo(List<ResourceCourseLivePo> resourceCourseLivePoList){
        if (CollectionUtil.isEmpty(resourceCourseLivePoList)) {
            return;
        }
        Set<Long> liveVideoIdSet = resourceCourseLivePoList.stream().map(ResourceCourseLivePo::getVideoId).filter(ObjectUtil::isNotNull).collect(Collectors.toSet());
        List<ResourceCourseVideoPo> resourceCourseVideoList = videoExtendService.listByIdList(liveVideoIdSet);
        if (CollectionUtil.isNotEmpty(resourceCourseVideoList)) {
            Map<Long, ResourceCourseVideoPo> resourceVideoMap = resourceCourseVideoList.stream().collect(Collectors.toMap(ResourceCourseVideoPo::getId, Function.identity()));
            resourceCourseLivePoList.forEach(liveCourse -> {
                ResourceCourseVideoPo resourceVideoPo = resourceVideoMap.getOrDefault(liveCourse.getVideoId(), new ResourceCourseVideoPo());
                liveCourse.setVideoUrl(resourceVideoPo.getUrl());
                liveCourse.setVideoDruation(resourceVideoPo.getDuration());
            });
        }
    }
}
